package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.domain.CheckItem;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.CheckItemService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 检查项控制层
 */
@RestController
@RequestMapping("/checkitem")
public class CheckItemController {
    @Reference
    private CheckItemService checkItemService;


    /**
     * 添加检查项
     *
     * @param checkItem
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody CheckItem checkItem) {
        try {
            checkItemService.add(checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed(MessageConstant.ADD_CHECKITEM_FAIL);
        }
        return Result.success(MessageConstant.ADD_CHECKITEM_SUCCESS);
    }


    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            PageResult page = checkItemService.findPage(queryPageBean);
            return Result.success(MessageConstant.QUERY_CHECKITEM_SUCCESS, page);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed(MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    /**
     * 删除检查项
     *
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    @PostMapping("/deleteCheckItem")
    public Result findPage(Integer id) {
        try {
            checkItemService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed(MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        return Result.success(MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    /**
     * 根据ID查找检查项
     *
     * @param id
     * @return
     */
    @PostMapping("/findById")
    public Result findById(Integer id) {
        try {
            CheckItem checkItem = checkItemService.findById(id);
            return Result.success(MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed(MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    /**
     * 修改检查项
     *
     * @param checkItem
     * @return
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody CheckItem checkItem) {
        try {
            checkItemService.update(checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed(MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        return Result.success(MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }


    /**
     * 查询所有检查项用于检查组展示
     *
     * @return
     */
    @PostMapping("/findAll")
    public Result findAll() {
        try {
            List<CheckItem> checkItemList = checkItemService.findAll();
            return Result.success(MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItemList);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed(MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }
}
